package com.team9.fitness.controller;

import com.team9.fitness.entity.FoodInfo;
import com.team9.fitness.service.FoodInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 食物信息控制器
 */
@RestController
@RequestMapping("/api/food-info")
@Slf4j
@CrossOrigin(origins = { "*" }, allowCredentials = "false")
public class FoodInfoController {

    @Autowired
    private FoodInfoService foodInfoService;

//    /**
//     * 创建食物信息
//     */
//    @PostMapping
//    public ResponseEntity<Map<String, Object>> createFoodInfo(@RequestBody FoodInfo foodInfo) {
//        Map<String, Object> response = new HashMap<>();
//
//        try {
//            FoodInfo createdFood = foodInfoService.createFoodInfo(foodInfo);
//            response.put("success", true);
//            response.put("message", "食物信息创建成功");
//            response.put("data", createdFood);
//            return ResponseEntity.ok(response);
//        } catch (Exception e) {
//            log.error("创建食物信息失败", e);
//            response.put("success", false);
//            response.put("message", "创建失败：" + e.getMessage());
//            return ResponseEntity.ok(response);
//        }
//    }

    /**
     * 获取所有食物信息
     */
    @GetMapping
    public ResponseEntity<Map<String, Object>> getAllFoodInfo() {
        Map<String, Object> response = new HashMap<>();

        try {
            List<FoodInfo> foodList = foodInfoService.getAllFoodInfo();
            response.put("success", true);
            response.put("data", foodList);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("获取食物信息列表失败", e);
            response.put("success", false);
            response.put("message", "获取失败：" + e.getMessage());
            return ResponseEntity.ok(response);
        }
    }

    /**
     * 根据ID获取食物信息
     */
    @GetMapping("/{id}")
    public ResponseEntity<Map<String, Object>> getFoodInfoById(@PathVariable Long id) {
        Map<String, Object> response = new HashMap<>();

        try {
            FoodInfo foodInfo = foodInfoService.getFoodInfoById(id);
            if (foodInfo != null) {
                response.put("success", true);
                response.put("data", foodInfo);
            } else {
                response.put("success", false);
                response.put("message", "食物信息不存在");
            }
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("获取食物信息失败", e);
            response.put("success", false);
            response.put("message", "获取失败：" + e.getMessage());
            return ResponseEntity.ok(response);
        }
    }

    /**
     * 根据分类获取食物信息
     */
    @GetMapping("/category/{category}")
    public ResponseEntity<Map<String, Object>> getFoodInfoByCategory(@PathVariable String category) {
        Map<String, Object> response = new HashMap<>();

        try {
            List<FoodInfo> foodList = foodInfoService.getFoodInfoByCategory(category);
            response.put("success", true);
            response.put("data", foodList);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("获取分类食物信息失败", e);
            response.put("success", false);
            response.put("message", "获取失败：" + e.getMessage());
            return ResponseEntity.ok(response);
        }
    }

    /**
     * 搜索食物信息
     */
    @GetMapping("/search")
    public ResponseEntity<Map<String, Object>> searchFoodInfo(@RequestParam String keyword) {
        Map<String, Object> response = new HashMap<>();

        try {
            List<FoodInfo> foodList = foodInfoService.searchFoodInfoByName(keyword);
            response.put("success", true);
            response.put("data", foodList);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("搜索食物信息失败", e);
            response.put("success", false);
            response.put("message", "搜索失败：" + e.getMessage());
            return ResponseEntity.ok(response);
        }
    }

    /**
     * 获取所有食物分类
     */
    @GetMapping("/categories")
    public ResponseEntity<Map<String, Object>> getAllCategories() {
        Map<String, Object> response = new HashMap<>();

        try {
            List<String> categories = foodInfoService.getAllCategories();
            response.put("success", true);
            response.put("data", categories);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("获取食物分类失败", e);
            response.put("success", false);
            response.put("message", "获取失败：" + e.getMessage());
            return ResponseEntity.ok(response);
        }
    }

//    /**
//     * 更新食物信息
//     */
//    @PutMapping("/{id}")
//    public ResponseEntity<Map<String, Object>> updateFoodInfo(@PathVariable Long id, @RequestBody FoodInfo foodInfo) {
//        Map<String, Object> response = new HashMap<>();
//
//        try {
//            foodInfo.setId(id);
//            FoodInfo updatedFood = foodInfoService.updateFoodInfo(foodInfo);
//            response.put("success", true);
//            response.put("message", "食物信息更新成功");
//            response.put("data", updatedFood);
//            return ResponseEntity.ok(response);
//        } catch (Exception e) {
//            log.error("更新食物信息失败", e);
//            response.put("success", false);
//            response.put("message", "更新失败：" + e.getMessage());
//            return ResponseEntity.ok(response);
//        }
//    }
//
//    /**
//     * 删除食物信息
//     */
//    @DeleteMapping("/{id}")
//    public ResponseEntity<Map<String, Object>> deleteFoodInfo(@PathVariable Long id) {
//        Map<String, Object> response = new HashMap<>();
//
//        try {
//            foodInfoService.deleteFoodInfo(id);
//            response.put("success", true);
//            response.put("message", "食物信息删除成功");
//            return ResponseEntity.ok(response);
//        } catch (Exception e) {
//            log.error("删除食物信息失败", e);
//            response.put("success", false);
//            response.put("message", "删除失败：" + e.getMessage());
//            return ResponseEntity.ok(response);
//        }
//    }
}
